package longrise.phone.com.bjjt_henan.main;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 年检提醒
 * Created by Administrator on 2017/5/11.
 */

public class CarInspectView extends LFView implements View.OnClickListener {

    private Context mContext;

    private View mView;

    private EntityBean bean;

    private TextView carnoTV;

    private LinearLayout infoLayout;

    private TextView dayTV;

    private TextView refreshTV;

    private TextView infoTV;

    private Button notifyBtn;

    private BaseApplication mApplication;

    private TextView ruleTV;

    private String days;

    private String carno;//车牌号

    public CarInspectView(Context context, EntityBean car) {
        super(context);
        this.bean = car;
        this.mContext = context;
        this.mApplication = ((MainActivity) context).getAppData();
        carno = bean.getString("carno");
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_carinspect, null);
        if (null == mView) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) mView.findViewById(R.id.tv_title);
        titleTV.setText(mContext.getResources().getString(R.string.as_notify));
        mView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        carnoTV = (TextView) mView.findViewById(R.id.carinspect_tv_carno);
        infoLayout = (LinearLayout) mView.findViewById(R.id.carinspect_layout);
        dayTV = (TextView) mView.findViewById(R.id.carinspect_tv_day);
        refreshTV = (TextView) mView.findViewById(R.id.carinspect_tv_refresh);
        infoTV = (TextView) mView.findViewById(R.id.carinspect_tv_info);
        notifyBtn = (Button) mView.findViewById(R.id.carinspect_btn);
        ruleTV = (TextView) mView.findViewById(R.id.carinspect_tv_rule);
        setEvent(true);

        long daytime = PrefUtils.getLong(mContext, carno, -1);
        if (daytime > 0) {//已设置提醒
            notifyBtn.setText("已设置提醒");
        }
    }

    private void showInfo(EntityBean carBean) {
        EntityBean dataBean = carBean.getBean("data");

        carnoTV.setText(dataBean.getString("carnumber"));
        days = dataBean.getString("days");
        dayTV.setText(days);
        infoTV.setText(carBean.getString("redes"));
        String type = dataBean.getString("type");
        if (TextUtils.equals(type, "1")) {//距离年检开始的时间
            infoLayout.setBackgroundResource(R.drawable.circle_bg);
            dayTV.setTextColor(mContext.getResources().getColor(R.color.lightyellow));
            refreshTV.setTextColor(mContext.getResources().getColor(R.color.lightyellow));
        } else if (TextUtils.equals(type, "2")) {//年检快要结束的时间
            infoLayout.setBackgroundResource(R.drawable.circle_other_bg);
            dayTV.setTextColor(mContext.getResources().getColor(R.color.notifyyellow));
            refreshTV.setTextColor(mContext.getResources().getColor(R.color.notifyyellow));
            notifyBtn.setVisibility(View.INVISIBLE);
        }
    }

    private void setEvent(boolean flag) {
        if (null != refreshTV) {
            refreshTV.setOnClickListener(flag ? this : null);
        }
        if (null != notifyBtn) {
            notifyBtn.setOnClickListener(flag ? this : null);
        }
        if (null != ruleTV) {
            ruleTV.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.carinspect_tv_refresh:
                carInspect();
                break;
            case R.id.carinspect_btn:
                setAlarm();
                break;
            case R.id.carinspect_tv_rule:
                InspectRuleView view = new InspectRuleView(mContext);
                FrameworkManager.getInstance().showNewForm(mContext, view);
                break;
            default:
                break;
        }
    }

    //设置定时闹钟提示年检时间
    private void setAlarm() {
        try {
            long daytimes = PrefUtils.getLong(mContext, carno, -1);
            if (daytimes > 0) {
                PrefUtils.setLong(mContext, carno, -1);
                notifyBtn.setText("提醒我");
                UiUtil.showToast(mContext, "取消提醒成功");
            } else {
                PrefUtils.setLong(mContext, carno, CommontUtils.getDistanceDate(Integer.valueOf(days)));
                notifyBtn.setText("已设置提醒");
                UiUtil.showToast(mContext, "设置提醒成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refresh() {
        carInspect();
    }

    //年检
    private void carInspect() {
        String cartype = bean.getString("cartype");
        String time = bean.getString("registertime");
        String uid = mApplication.getKckpName();
        String token = mApplication.getToken();
        DialogUtils.showDialog(mContext);
        ApiImpI.getInstance(mContext).carInspect(carno, time, cartype, uid, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(mContext, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                if (mView == null) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    EntityBean dataBean = bean.getBean("data");
                    if (dataBean != null) {
                        showInfo(bean);
                        return;
                    }
                }
                String redes = bean.getString("redes");
                if (!TextUtils.isEmpty(redes)) {
                    UiUtil.showToast(getContext(), redes);
                }
            }
        });
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        setEvent(false);
    }
}
